/**
 * 知识库 - 编辑页面
 * @author yuan_rz
 * @date 2019/12/24 18:03
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        vueData = {
            isAdd: false,
            uploadUrl: g_ctxPath + 'upload/imgs',
            imageList: [],
            form: '',
            rules: {
                MMRZ001: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                MMRZ002: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                MMRZ003: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                MMRZ004: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                MMRZ006: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                MMRZ007: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                MMRZ008: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                MMRZACTI: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ]
            },
            selectOptions: {
                sys_y_n: app.datas_('sys_y_n')
            }
        },
        callObj
    ;

    var gwai820Edit = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwai820_edit',
                _loadTplsFull: [{
                    dom: 'common_open_window', tpl: 'common/_open_window'
                }]
            }, win);
        },
        editData: function () {
            app.ajax({
                xurl: 'crm/gwai820/editData',
                xdata: vueData.form,
                loading: true,
                okmsg: self._moduleName + '信息修改成功',
                cb: function (result) {
                    if (result.ok) {
                        app.pageModule('gwai820').reloadTableCurr();
                        self._closeTpl();
                    }
                }
            });
        },
        addData: function () {
            app.ajax({
                xurl: 'crm/gwai820/addData',
                xdata: vueData.form,
                loading: true,
                okmsg: self._moduleName + '信息保存成功',
                cb: function (result) {
                    if (result.ok) {
                        app.pageModule('gwai820').reloadTableCurr();
                        self._closeTpl();
                    }
                }
            });
        },
        _call: function (tagData) {
            self.vues[0].$refs.upload.clearFiles();
            if (tagData.is_add) {
                vueData.isAdd = true;
                app.extendUpLow({
                    target: vueData.form,
                    res: {
                        MMRZACTI: 'Y'
                    },
                    reset: true
                });
            } else {
                vueData.isAdd = false;
                callObj = tagData.obj;
                app.extendUpLow({
                    target: vueData.form,
                    res: callObj.data,
                    reset: true
                });
                if (vueData.form.MMRZ005 != null && vueData.form.MMRZ005 != '') {
                    vueData.imageList.removeAll();
                    vueData.imageList.push({
                        name: '故障图片.jpg', url: '/wissun-smfile/' + vueData.form.MMRZ005
                    });
                }
            }

        },
        _ready: function () {
            vueData.form = app.fields_(['MMRZ_T']);
            vueData.form.MMRZ001_NAME = '';
            vueData.form.MMRZ006_NAME = '';
            vueData.form.MMRZ008_NAME = '';
            this._super({
                vue: [{
                    el: '#gwai820_edit',
                    data: vueData,
                    methods: {
                        openFaultWindow: function () {
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method: 'q_mmrl001',
                                        columnList: [
                                            {label: '故障编号', prop: 'MMRL001', width: '150'},
                                            {label: '说明', prop: 'MMRLL003', width: '150'},
                                            {label: '助记码', prop: 'MMRLL004', width: '150'}
                                        ]
                                    },
                                    cb: function (data) {
                                        vueData.form.MMRZ001 = data.MMRL001;
                                        vueData.form.MMRZ001_NAME = data.MMRLL003;
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择故障',
                                    area: ['600px', '465px']
                                }
                            });
                        },
                        openStaffWindow: function () {
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method: 'q_sysuser_id',
                                        columnList: [
                                            {label: '维修人员ID', prop: 'ID', width: '150'},
                                            {label: '维修人员账号', prop: 'ACCOUNT', width: '150'},
                                            {label: '维修人员名称', prop: 'NAME', width: '150'}
                                        ]
                                    },
                                    cb: function (data) {
                                        vueData.form.MMRZ006 = data.ID;
                                        vueData.form.MMRZ006_NAME = data.NAME;
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择技术人员',
                                    area: ['600px', '465px']
                                }
                            });
                        },
                        openOrganizeWindow: function () {
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method: 'q_sysorg_id',
                                        columnList: [
                                            {label: '部门编号', prop: 'ID', width: '150'},
                                            {label: '部门名称', prop: 'NAME', width: '150'}
                                        ]
                                    },
                                    cb: function (data) {
                                        vueData.form.MMRZ008 = data.ID;
                                        vueData.form.MMRZ008_NAME = data.NAME;
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择归属部门',
                                    area: ['600px', '465px']
                                }
                            });
                        },
                        beforeUpload: function (file) {
                            var isJPG = file.type.includes("image");
                            var isLt2M = file.size / 1024 / 1024 < 2;
                            var imgFile = new FileReader();
                            if (isJPG && isLt2M) imgFile.readAsDataURL(file);
                            if (!isJPG) this.$message.error("上传文件必须是图片格式!")
                            if (!isLt2M) this.$message.error("上传图片大小不能超过 2MB!");
                            return isJPG && isLt2M;
                        },
                        successUpload: function (response) {
                            if (response.code == 200) {
                                vueData.form.MMRZ005 = response.data[0].name;
                            }
                        },
                        cancel: function () {
                            self._closeTpl();
                        },
                        sure: function () {
                            this.$refs['form'].validate(function (valid) {
                                if (valid) {
                                    var reg=/^1[3456789]\d{9}$/;
                                    if(!reg.test(vueData.form.MMRZ007)){
                                        app.showWarning("手机号码格式错误，请重新填写！");
                                        return;
                                    }
                                    if (vueData.isAdd) {
                                        self.addData();
                                    } else {
                                        self.editData();
                                    }
                                }
                            });
                        }
                    },
                    created: function () {
                        self._setEleIsReady('gwai820_edit');
                    }
                }]
            });
        },
        _lazy: function () {
            this._super();
        }
    });

    return new gwai820Edit();

});
